'use strict';

const Controller = require('egg').Controller;

// 挖矿2合约
class Mining2Controller extends Controller {
  // 扫描质押事件
  async scanDepositMining2() {
    const { app, ctx, service } = this;
    // 开始时间, 结束时间
    const min_timestamp = ctx.request.body.startTime;
    const max_timestamp = ctx.request.body.endTime;
    const event_name = 'Deposit';
    const res = await service.mining2.scanEvent2(event_name, min_timestamp, max_timestamp);
    const data = [];
    for (let i = 0; i < res.length; i++) {
      let d = {
        blockNumber: res[i].block_number,
        blockTime: res[i].block_timestamp,
        eventName: res[i].event_name,
        transaction: res[i].transaction_id,

        poolId: res[i].result.pid,
        userAddress0x: res[i].result.user,
        value: res[i].result.amount,
        userAddress: await service.eth.addressFromHex(res[i].result.user),
      };
      data.push(d);
    }

    const data2 = data.sort(function(a,b){return a.blockNumber - b.blockNumber;});
    ctx.body = {code: 1000, msg: '请求成功', data: data2};
  }

  // 扫描提现事件
  async scanWithdrawMining2() {
    const { app, ctx, service } = this;
    // 开始时间, 结束时间
    const min_timestamp = ctx.request.body.startTime;
    const max_timestamp = ctx.request.body.endTime;
    const event_name = 'Withdraw';
    const res = await service.mining2.scanEvent2(event_name, min_timestamp, max_timestamp);
    const data = [];
    for (let i = 0; i < res.length; i++) {
      let d = {
        blockNumber: res[i].block_number,
        blockTime: res[i].block_timestamp,
        eventName: res[i].event_name,
        transaction: res[i].transaction_id,

        poolId: res[i].result.pid,
        userAddress0x: res[i].result.user,
        value: res[i].result.amount,
        userAddress: await service.eth.addressFromHex(res[i].result.user),
      };
      data.push(d);
    }

    const data2 = data.sort(function(a,b){return a.blockNumber - b.blockNumber;});
    ctx.body = {code: 1000, msg: '请求成功', data: data2};
  }




}

module.exports = Mining2Controller;
